package com.deng.api.controller;

import com.deng.api.entity.CloudChannel;
import com.deng.api.service.ICloudChannelService;
import com.deng.api.vo.PageVO;
import com.deng.commons.common.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author deng
 * @since 2022-08-10
 */
@RestController
@Api("渠道来源管理")
@RequestMapping("/api/channel")
public class CloudChannelController {
    @Resource
    private ICloudChannelService iCloudChannelService;

    @ApiOperation("返回全部的渠道")
    @GetMapping(value = "/all")
    public BaseResult getAllChannel(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            CloudChannel c) {
        if (c == null) {
            c = new CloudChannel();
        }
        c.setChannelId(0);
        PageVO pageVO = iCloudChannelService.getAll(page, size, c);
        return BaseResult.success(pageVO);
    }

    @ApiOperation("返回全部的来源")
    @GetMapping(value = "/source")
    public BaseResult getAllSource(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            CloudChannel c) {
        PageVO pageVO;
        if (c.getId() == null || c.getId() == 0) {
            CloudChannel c2 = iCloudChannelService.getFirstChannel();
            pageVO = iCloudChannelService.getAll(page, size, new CloudChannel(c2.getId()));
        } else {
            pageVO = iCloudChannelService.getAll(page, size, new CloudChannel(c.getId()));
        }
        return BaseResult.success(pageVO);
    }
}
